package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneService;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <Description> 业务号码 <br>
 *
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.attr.handler <br>
 * @since R9.0<br>
 */
@Service
public class PostponeInfoNumAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        IQuerySceneService querySceneService = SpringUtil.getBean(IQuerySceneService.class);
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        KiteSceneInst curSceneInst = querySceneService.getSceneInst(data.getSceneInstId());
        KiteOfferInst offerInst = querySceneInstService.getOfferInst(data.getCustId(), curSceneInst.getRootInstId());
        List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInstsFromOfferInst(data.getCustId(), offerInst.getOfferInstId());
        String serviceOfferId = data.getServiceOfferId();
        String value = attr.getValue();
        String fieldName = attr.getFieldName();
        if(KiteStringUtils.equals("200002981",fieldName) && KiteStringUtils.isEmpty(value)){
            if (inst instanceof KiteProdInstFunc) {
                KiteProdInstFunc kiteProdInstFunc = (KiteProdInstFunc)inst;
                String prodInstId = kiteProdInstFunc.getAccProdInstId();
                Map<String, String> instSerialNumberMap = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "instSerialNumberMap");
                if(KiteMapUtils.isNotEmpty(instSerialNumberMap)){
                    String accNum = KiteMapUtils.getString(instSerialNumberMap,prodInstId);
                    if(KiteStringUtils.equals(LocalServiceOfferConsts.SERVICE_OFFER_DELAY,serviceOfferId) && KiteStringUtils.isEmpty(value)){
                        if(!KiteStringUtils.isEmpty(accNum)){
                            attr.setValue(accNum);
                            super.set(attr, inst, accNum, data);
                        }
                    }
                }
            }
        }
        if(KiteStringUtils.equals("200002772",fieldName) && KiteStringUtils.isEmpty(value)){
            for(KiteProdInst prodInst : kiteProdInsts){
                KiteAttr kiteAttr = prodInst.getAttr("200002772");
                if(!KiteObjectUtils.isEmpty(kiteAttr)){
                    List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
                    if(KiteListUtils.isNotEmpty(attrValues)){
                        attr.setAttrValues(attrValues);
                        attr.setValue(kiteAttr.getValue());
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
                        break;
                    }
                }
            }
        }
    }
}
